<template>

    <div>

        <div class="page-header">
            <h1>
                <router-link to="/business/course">
                    {{courseinfo.name}}
                </router-link>
                <small>
                    <i class="ace-icon fa fa-angle-double-right"></i>
                    <router-link to="/business/chapter">
                        {{chapterinfo.name}}
                    </router-link>
                </small>
            </h1>
        </div>

        <p>
            <router-link to="/business/chapter" class="btn btn-white btn-round btn-default">
                <i class="ace-icon fa fa-arrow-left"></i>
                返回大章
            </router-link>
            <button @click="list" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh green"></i>
                刷新
            </button>
            &nbsp;
            <button @click="add" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-edit red"></i>
                新增
            </button>
        </p>
        <pagination ref="pagination" v-bind:list="list"></pagination>
        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                        <th>ID</th>

                        <th>标题</th>

                        <th>VOD</th>

                        <th>时长</th>

                        <th>收费</th>

                        <th>顺序</th>


                <th>操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="section in sections">
                    <td>{{section.id}}</td>
                    <td>{{section.title}}</td>
                    <td>{{section.vod}}</td>
                    <td>{{$filters.formatSecond(section.time)}}</td>
                    <td>{{$filters.optionKV(section_charge,section.charge)}}</td>
                    <td>{{section.sort}}</td>

                <td>
                    <div class="hidden-sm hidden-xs btn-group">
                        <button @click="play(section.vod)" class="btn btn-white btn-sm btn-success">
                            <i class="ace-icon fa fa-video-camera bigger-120"></i>播放
                        </button>

                        <button @click="edit(section)" class="btn btn-white btn-sm btn-info">
                            <i class="ace-icon fa fa-pencil bigger-120"></i>编辑
                        </button>

                        <button @click="handledelete(section.id)" class="btn btn-white btn-sm btn-danger">
                            <i class="ace-icon fa fa-trash-o bigger-120"></i>删除
                        </button>

                    </div>

                    <div class="hidden-md hidden-lg">
                        <div class="inline pos-rel">
                            <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown"
                                    data-position="auto">
                                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                            </button>

                            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                                                <li>
                                                                    <a @click="play(section.vod)" class="tooltip-info" data-rel="tooltip" title="播放">
                                										<span class="blue">
                                										<i class="ace-icon fa fa-search-plus bigger-120"></i>
                                                                        </span>
                                                                    </a>
                                                                </li>

                                <li>
                                    <a @click="edit(section)" class="tooltip-success" data-rel="tooltip" title="编辑">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                    </a>
                                </li>

                                <li>
                                    <a @click="handledelete(section.id)" class="tooltip-error" data-rel="tooltip"
                                       title="删除">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
        <!-- /.span -->

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">标题:</label>
                                    <div class="col-sm-10">
                                        <input type="text" v-model="sectioninfo.title" class="form-control" placeholder="标题">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">课程:</label>
                                    <div class="col-sm-10">
                                        <p class="form-control-static">{{courseinfo.name}}</p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">大章:</label>
                                    <div class="col-sm-10">
                                        <p class="form-control-static">{{chapterinfo.name}}</p>
                                    </div>
                                </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">视频:</label>
                                <div class="col-sm-10">
                                    <vod v-bind:text="'上传VOD'" v-bind:input-id="'image-upload'"
                                          v-bind:after-upload="afterUploadSection"
                                          v-bind:suffixs="['mp4','mpeg','avi']"
                                          v-bind:use="FILE_USE.COURSE.key"></vod>
                                    <div v-show="sectioninfo.video" class="row">
                                        <div class="col-md-9">
                                            <player v-bind:player-id="'form-player-div'" ref="player"></player>
                                            <video id="video" v-bind:src="sectioninfo.video" controls="controls" hidden/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">时长:</label>
                                    <div class="col-sm-10">
                                        <input type="text" v-model="sectioninfo.time" class="form-control" placeholder="时长">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">视频:</label>
                                    <div class="col-sm-10">
                                        <input type="text" v-model="sectioninfo.video" class="form-control" placeholder="视频" disabled>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">VOD:</label>
                                    <div class="col-sm-10">
                                        <input type="text" v-model="sectioninfo.vod" class="form-control" placeholder="vod" disabled>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">收费:</label>
                                    <div class="col-sm-10">
                                        <select v-model="sectioninfo.charge" class="form-control">
                                            <option v-for="o in section_charge" v-bind:value="o.key">{{o.value}}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">顺序:</label>
                                    <div class="col-sm-10">
                                        <input type="text" v-model="sectioninfo.sort" class="form-control" placeholder="顺序">
                                    </div>
                                </div>

                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" @click="save" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

    <modal-player ref="modalPlayer"></modal-player>
    </div><!-- /.row -->


</template>


<script scope>
    import axios from 'axios';
    import {defineComponent, onMounted, ref} from 'vue';
    import Pagination from '@/components/pagination.vue';
    import BigFile from '@/components/big-file.vue';
    import Vod from '@/components/vod.vue';
    import OssBigFile from '@/components/oss-big-file.vue';
    import Player from '@/components/player.vue';
    import router from "../../router";
    import ModalPlayer from "../../components/modal-player";

    export default defineComponent({
        name: 'section',
        components:{
            ModalPlayer,
            Pagination,BigFile,OssBigFile,Vod,Player
        },
        setup() {
            const sections = ref();
            const player=ref({});
            const modalPlayer=ref({});
            const pagination = ref({});
            const sectioninfo=ref({});
            let courseinfo=ref({});
            let chapterinfo=ref({});
            const section_charge=SECTION_CHARGE;


            const list = () => {
                Loading.show();
                axios.get("/business/admin/section/list", {
                    params: {
                        page: pagination.value.page,
                        size: pagination.value.size,
                        chapterId: chapterinfo.value.id,
                    }
                }).then((response) => {
                    Loading.hide();
                    const data=response.data;
                    if (data.success){
                        sections.value = data.content.list;
                        pagination.value.render(pagination.value.page,data.content.total);
                    }else {
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            const add=()=>{
                sectioninfo.value={};
                $('#myModal').modal();
            };

            /**
             * 编辑
             */
            const edit = (section) => {
                $('#myModal').modal();
                sectioninfo.value = Tool.copy(section);
            };

            /**
             * 删除
             */
            const handledelete = (id) => {
                Confirm.show("删除小节后不可恢复，确认删除？", function () {
                    Loading.show();
                    axios.delete("/business/admin/section/delete/" + id).then((response) => {
                        Loading.hide();
                        const data = response.data;
                        if (data.success) {
                            list();
                            Toast.success("删除成功!");
                        }else{
                            Loading.hide();
                            Toast.warning(data.message);
                        }
                    })
                });
            };

            const save=()=>{

                sectioninfo.value.video="";

                // 保存校验
                if (1 != 1
                    || !Validator.require(sectioninfo.value.title, "标题")
                    || !Validator.length(sectioninfo.value.title, "标题", 1, 50)
                    || !Validator.length(sectioninfo.value.video, "视频", 1, 255)
                ) {
                    return;
                }

                //赋值给chapterid和课程id
                sectioninfo.value.courseId=courseinfo.value.id;
                sectioninfo.value.chapterId=chapterinfo.value.id;


                Loading.show();
                axios.post("/business/admin/section/save",sectioninfo.value).then((response) => {
                    const data=response.data;
                    if (data.success){
                        Loading.hide();
                        $('#myModal').modal('hide');
                        // console.log(data);
                        list();
                        Toast.success("保存成功！");
                    }else{
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            /*
           * 获取从课程和大章界面传值的信息
           * */
            const getCourseandChapter=()=>{
                let course=SessionStorage.get(SESSION_KEY_COURSE) || {};
                let chapter=SessionStorage.get(SESSION_KEY_CHAPTER) || {};
                if(Tool.isEmpty(course) || Tool.isEmpty(chapter)){
                    router.push("/welcome");
                }
                courseinfo.value=course;
                chapterinfo.value=chapter;
            }

            const afterUploadSection=(data)=>{
                let _this=this;
                let video=data.content.path;
                let vod=data.content.vod;
                sectioninfo.value.video=video;
                sectioninfo.value.vod=vod;
                getTime();
                player.value.playUrl(video);
            }

            /*
            * 获取视频时长
            * */
            const getTime=()=>{
                let ele=document.getElementById("video");
                //当视频可以播放的时候去获取时长，否则可能获取不到值。
                ele.oncanplay=function(){
                    //将duration时长换算成十进制整数=>秒
                    sectioninfo.value.time=parseInt(ele.duration,10);
                }
            }

            /*
            * 播放视频
            * */
            const play=(vod)=>{
                modalPlayer.value.playVod(vod);
            }


            onMounted(() => {
                getCourseandChapter();
                list();
            });

            return {
                list,
                add,
                section_charge,
                edit,
                handledelete,
                pagination,
                sectioninfo,
                save,
                sections,
                getCourseandChapter,
                courseinfo,
                chapterinfo,
                afterUploadSection,
                FILE_USE,
                player,
                play,
                modalPlayer
            }


        }
    });
</script>

<style scoped>
    .page-header{
        margin: 0 0 12px;
        border-bottom: 1px dotted #E2E2E2;
        padding-bottom: 16px;
        padding-top: 7px;
    }
    .page-header h1 {
        padding: 0;
        margin: 0 8px;
        font-size: 24px;
        font-weight: lighter;
        color: #2679B5;
    }
    .page-header h1 small {
        margin: 0 6px;
        font-size: 14px;
        font-weight: 400;
        color: #8089A0;
    }
    video{
        width: 100%;
        height: auto;
        margin-top: 10px;
    }

</style>